import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { STColumn, STColumnBadge, STComponent, STData } from '@delon/abc/st';
import { SFComponent, SFDateWidgetSchema, SFSchema, SFUISchema } from '@delon/form';
import { ModalHelper } from '@delon/theme';
import { DynamicSettingModalComponent, SearchDrawerService } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { BasicTableComponent } from 'src/app/routes/commom';
import { AccountDetailComponent } from 'src/app/shared/components/account-detail/account-detail.component';
import { UsermanageService } from '../../services/usercenter.service';
import { CarSettleAddDriverComponent } from './add-driver/add-driver.component';
@Component({
  selector: 'app-usercenter-components-driver',
  styleUrls: ['../../../commom/less/commom-table.less'],
  templateUrl: './driver.component.html'
})
export class UserCenterComponentsDriverComponent extends BasicTableComponent implements OnInit {
  schema: SFSchema = this.initSF();
  columns: STColumn[] = this.initST();

  @ViewChild('promoterModal', { static: false })
  promoterModal!: any;
  promotersTelephone = '';

  resourceStatus: any = '';
  constructor(
    public service: UsermanageService,
    private modal: NzModalService,
    private router: Router,
    private ar: ActivatedRoute,
    private modalHelper: ModalHelper,
    public searchDrawerService: SearchDrawerService
  ) {
    super(searchDrawerService);
  }

  search() {
    this.st?.load(1);
  }

  /**
   * 查询参数
   */
  get reqParams() {
    const params: any = {
      ...(this.sf && this.sf?.value),
      auditStatus: this.resourceStatus
    };
    if (this.sf?.value.effectiveDate) {
      Object.assign(params, {
        createTime: {
          start: this.sf?.value.effectiveDate[0],
          end: this.sf?.value.effectiveDate[1]
        }
      });
      // params.effectiveDateStart = this.sf?.value.effectiveDate[0];
      // params.effectiveDateEnd = this.sf?.value.effectiveDate[1];
    }
    delete params.effectiveDate;
    delete params.expand;
    return params;
  }

  ngOnInit() {}

  selectChange(e: any) {
    this.resourceStatus = e;
    setTimeout(() => {
      this.st.load();
    }, 200);
  }

  addPromoter(item?: any) {
    this.promotersTelephone = item?.promotersTelephone;
    const modal = this.modal.create({
      nzTitle: '推广业务员',
      nzContent: this.promoterModal,
      nzOnOk: () => {
        if (!!!this.promotersTelephone) {
          this.service.msgSrv.error('请填写手机号!');
          return false;
        }
        if (typeof this.promotersTelephone === 'string' && !/(^1\d{10}$)/.test(this.promotersTelephone)) {
          this.service.msgSrv.error('手机格式错误');
          return false;
        }
        this.service
          .request(this.service.$api_add_user_salesman, { userId: item.userId, mobile: this.promotersTelephone })
          .subscribe(res => {
            if (res) {
              this.service.msgSrv.success(item?.promotersTelephone ? '添加推广员成功' : '修改推广员成功');
            }
            this.st.load();
          });
        return;
      }
    });
  }

  settingAction(item?: any) {
    this.modal.create({
      nzTitle: '基础设置',
      nzContent: DynamicSettingModalComponent,
      nzWidth: 900,
      nzComponentParams: {
        extendType: '3',
        businessId: item.appUserId
      },
      nzFooter: null
    });
  }

  showAccountDetail(item: any) {
    console.log(item);

    this.modal.create({
      nzTitle: '资金账户',
      nzContent: AccountDetailComponent,
      nzNoAnimation: true,
      nzWidth: 600,
      nzComponentParams: {
        isCanCreate: true,
        url: '/api/fcc/accountBalance/getDriverAccountDetailByOperator',
        params: { accountType: 2, roleId: item.appUserId, ctfId: item.identityNo, clientName: item.name }
      },
      nzFooter: null
    });
  }

  exportList() {
    const params = this.reqParams;
    this.service.downloadFile(this.service.$api_export_driver, { ...params, pageSize: -1 });
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        name: { title: '司机姓名', type: 'string', ui: { placeholder: '请输入', showRequired: false } },
        mobile: {
          title: '手机号',
          type: 'string',
          maxLength: 11,
          ui: {
            placeholder: '请输入'
          }
        },
        identityStatus: {
          type: 'string',
          title: '实名状态',
          enum: [
            { label: '全部', value: '' },
            { label: '待审核', value: 0 },
            { label: '通过', value: 1 },
            { label: '驳回', value: 2 }
          ],
          default: '',
          ui: {
            widget: 'select'
          }
        },
        driverLicenseStatus: {
          type: 'string',
          title: '驾驶证状态',
          enum: [
            { label: '全部', value: '' },
            { label: '待审核', value: 10 },
            { label: '审核通过', value: 20 },
            { label: '驳回', value: 30 },
            { label: '证件过期', value: 40 }
          ],
          default: '',
          ui: {
            widget: 'select'
          }
        },
        practiceSenioritLicenseStatus: {
          type: 'string',
          title: '从业资格证状态',
          enum: [
            { label: '全部', value: '' },
            { label: '未提交 ', value: -1 },
            { label: '已提交', value: 1 },
            { label: '已通过', value: 2 },
            { label: '已过期', value: 3 }
          ],
          default: '',
          ui: {
            widget: 'select'
          }
        },
        promotersTelephone: {
          title: '推广业务员',
          type: 'string',
          maxLength: 11,
          ui: {
            placeholder: '请输入手机号'
          }
        },
        source: {
          type: 'string',
          title: '注册渠道',
          enum: [
            { label: '全部', value: '' },
            { label: '用户注册', value: 1 },
            { label: '货主添加', value: 2 },
            { label: '运营添加', value: 3 }
          ],
          default: '',
          ui: {
            widget: 'select'
          }
        },
        effectiveDate: {
          title: '申请时间',
          type: 'string',
          ui: {
            widget: 'date',
            mode: 'range',
            format: 'yyyy-MM-dd'
          } as SFDateWidgetSchema
        }
      }
    };
  }
  add() {
    this.modalHelper.create(CarSettleAddDriverComponent, { size: 900 }).subscribe(res => {
      this.st.load();
    });
  }
  private initST(): STColumn[] {
    return [
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
        } 
      },
      { title: '司机姓名', className: 'text-center', index: 'name', width: 150 },
      { title: '手机号', className: 'text-center', index: 'mobile', width: 150 },
      { title: '身份证号码', className: 'text-center', index: 'identityNo', width: 200 },
      { title: '当前车辆', className: 'text-center', render: 'carNo', width: 200 },
      { title: '驾驶证审核人', className: 'text-center', index: 'approvalUserName', width: 200 },
      { title: '审核时间', className: 'text-center', index: 'approvalTime', width: 200 },
      {
        title: '实名认证状态',
        className: 'text-center',
        index: 'identityStatus',
        type: 'badge',
        badge: {
          '-1': { text: '未提交', color: 'default' },
          0: { text: '待审核', color: 'default' },
          1: { text: '通过', color: 'success' },
          2: { text: '驳回', color: 'warning' }
        },
        width: 180
      },
      {
        title: '驾驶证状态',
        className: 'text-center',
        index: 'driverLicenseStatus',
        type: 'badge',
        badge: {
          '-1': { text: '未提交', color: 'default' },
          10: { text: '待审核', color: 'default' },
          20: { text: '审核通过', color: 'success' },
          30: { text: '驳回', color: 'warning' },
          40: { text: '证件过期', color: 'error' }
        },
        width: 180
      },
      {
        title: '从业资格证状态',
        className: 'text-center',
        index: 'practiceSenioritLicenseStatus',
        type: 'badge',
        badge: {
          '-1': { text: '未提交', color: 'default' },
          1: { text: '已提交', color: 'processing' },
          2: { text: '已通过', color: 'success' },
          3: { text: '已过期', color: 'error' }
          // 10: { text: '待审核', color: 'default' },
          // 20: { text: '审核通过', color: 'success' },
          // 30: { text: '驳回', color: 'warning' },
          // 40: { text: '证件过期', color: 'error' }
        },
        width: 180
      },
      { title: '推广业务员', className: 'text-center', render: 'promotersTelephone', width: 180 },
      {
        title: '注册渠道',
        className: 'text-center',
        index: 'source',
        type: 'enum',
        enum: { 1: '用户注册', 2: '货主添加', 3: '运营添加' },
        width: 150
      },
      { title: '注册时间', className: 'text-center', index: 'createTime', width: 200 },
      {
        title: '操作',
        width: '110px',
        className: 'text-center',
        fixed: 'right',
        buttons: [
          {
            text: '查看<br/>',
            click: item => {
              this.router.navigate(['./detail', item.appUserId], {
                relativeTo: this.ar,
                queryParams: {
                  status: item.driverLicenseStatus
                }
              });
            },
            acl: { ability: ['USERCENTER-DRIVER-LIST-view'] }
          },
          // {
          //   text: '基础设置',
          //   click: item => this.settingAction(item),
          //   acl: { ability: ['USERCENTER-DRIVER-LIST-basicSetting'] },
          // },
          {
            text: '资金账户',
            click: item => this.showAccountDetail(item),
            acl: { ability: ['USERCENTER-DRIVER-LIST-account'] }
          }
        ]
      }
    ];
  }
  viewCar(item: any) {
    this.router.navigate(['/vehicle/list/detail/' + item.carId]);
  }
}
